$(function () {
    showModel();
    hideModel();
    addStudent();
    delStudent();
    showEditModel();
    hideEditModel();
    editStudent();
});


function showModel() {
    $("#model_add_student_btn").click(function () {
        $("#stuShadow").show();
        $("#add_student_model").show();
        // 向后端请求所有班级列表
        $.ajax({
            url: '/js_get_class_list/',
            type: 'get',
            dataType: 'JSON',
            success: function (data) {
                for (each of data) {
                    var option = document.createElement("option");
                    option.innerHTML = each.cname;
                    option.setAttribute('value', each.id);
                    $("#class_id").append(option);
                }
            }

        });
    });
}

function hideModel() {
    $("#cancel_model_add_student_btn").click(function () {
        $("#stuShadow").hide();
        $("#add_student_model").hide();
    });
}

function addStudent() {
    $("#add_student_btn").click(function () {
        $.ajax({
            url: '/model_add_student/',
            type: 'POST',
            data: {'sname': $("#sname").val(), 'class_id': $("#class_id").val()},
            dataType: 'JSON',
            success: function (data) {
                if (data.status) {
                    location.reload();
                } else {
                    alert(data.msg);
                }
            },
        });
    });

}

function delStudent(){
    $(".model_del_student_btn").click(function(){
        var sid = $(this).attr("sid") ;
        $.ajax({
            url:'/model_del_student/',
            type:'POST',
            data:{"sid":sid},
            dataType:'JSON',
            success:function(data){
                if(data.status){
                    location.reload();
                }
                else{
                    alert(data.msg);

                }
            }
        })
    });
}


function showEditModel(){
    $(".model_edit_student_btn").click(function(){
        $("#stuShadow").show();
        $("#edit_student_model").show();
        // 显示修改学生的姓名和他所在的班级
        $.ajax({
           url:'/js_get_one_student/?sid='+$(this).attr('sid'),
           type:'GET',
           dataType:'JSON',
           success:function(data){
               $("#edit_sname").val(data.one_student.sname);
               $("#edit_sid").val(data.one_student.id)
               for (each of data.class_list){
                   var opt = document.createElement("option");
                   opt.innerHTML = each.cname;
                   opt.value = each.id;
                   if(data.one_class.cname == each.cname){
                       opt.selected = true;
                   }else{
                       opt.selected = false;
                   }
                   $("#edit_class_id").append(opt);
               }
           }
        });
    });

}

function hideEditModel(){
    $("#cancel_model_edit_student_btn").click(function(){
        $("#stuShadow").hide();
        $("#edit_student_model").hide();

    });
}

function editStudent(){
    $("#edit_student_btn").click(function(){
        sid = $("#edit_sid").val();
        sname = $("#edit_sname").val();
        class_id = $("#edit_class_id").val();
        $.ajax({
            url:'/model_edit_student/',
            type:'POST',
            data:{'sid':sid, 'sname':sname, 'class_id':class_id},
            dataType:'JSON',
            success:function (data){
                if(data.status){
                    location.reload();
                }
                else{
                    alert(data.msg);
                }
            }
        })
    });
}